package com.qiaofang.collectmanage.dao.impl;
import com.qiaofang.collectmanage.dao.domain.CrawlerTask;
import com.qiaofang.collectmanage.dao.mapper.CrawlerTaskMapper;
import com.qiaofang.collectmanage.dao.CrawlerTaskDao;
import com.qiaofang.collectmanage.stub.constants.DeletedEnum;
import com.qiaofang.common.model.page.PageDTO;
import com.qiaofang.common.page.PageUtil;
import com.qiaofang.collectmanage.stub.bean.SearchCrawlerTaskDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;
import tk.mybatis.mapper.entity.Example;
import org.apache.ibatis.session.RowBounds;
import tk.mybatis.mapper.entity.Example;
/**
* Created by generator on 2018-8-1 13:49:21
*/
@Repository("crawlerTaskDao")
public class CrawlerTaskDaoImpl implements CrawlerTaskDao{

    @Autowired
    private CrawlerTaskMapper taskMapper;

    @Override
    public CrawlerTask  getCrawlerTaskByUuid(String uuid){
       CrawlerTask  task = new CrawlerTask();
       task.setUuid(uuid);
        return taskMapper.selectOne(task);
   }


    @Override
    public CrawlerTask getCrawlerTaskByCrawlerTask(CrawlerTask task){
        return taskMapper.selectOne(task);
    }

    @Override
    public int saveCrawlerTask(CrawlerTask task) {
         return taskMapper.insertSelective(task);
    }

    @Override
    public int  updateCrawlerTask (CrawlerTask task) {
        Example ex = new Example(CrawlerTask.class);
         ex.createCriteria()
         .andIsNotNull("uuid")
         .andEqualTo("uuid",task.getUuid());
        return  taskMapper.updateByExampleSelective(task,ex);
    }

    @Override
    public int removeCrawlerTaskByUuid (String uuid){
        Example ex = new Example(CrawlerTask.class);
        ex.createCriteria().andEqualTo("uuid",uuid);
        CrawlerTask task = new  CrawlerTask();
        task.setDeleted(DeletedEnum.YES.getValue());
        return  taskMapper.updateByExampleSelective(task,ex);
    }


    @Override
    public List<CrawlerTask> listCrawlerTask(CrawlerTask task){
          if(task == null){
              task  = new CrawlerTask();
          }
         task.setDeleted(DeletedEnum.NO.getValue());
          return taskMapper.select(task);
    }


        @Override
        public List<CrawlerTask> listCrawlerTaskByUuids(List<String> uuidList){
           Example ex = new Example(CrawlerTask.class);
           ex.createCriteria().andIn("uuid",uuidList);
           return taskMapper.selectByExample(ex);
        }

    @Override
    public List<CrawlerTask> listCrawlerTaskByIds(List<Integer> idList){
        Example ex = new Example(CrawlerTask.class);
        ex.createCriteria().andIn("id",idList);
        return taskMapper.selectByExample(ex);
    }

    @Override
    public  Long countCrawlerTask(CrawlerTask task){
        if(task == null){
          task  = new CrawlerTask();
        }
         task.setDeleted(DeletedEnum.NO.getValue());
         return Long.valueOf(taskMapper.selectCount(task));
    }

    @Override
    public List<CrawlerTask> searchCrawlerTaskBySimpleAttr(CrawlerTask task,RowBounds rb){
        if(task == null){
           task  = new CrawlerTask();
        }
         task.setDeleted(DeletedEnum.NO.getValue());
         return taskMapper.selectByRowBounds(task,rb);
    }

    @Override
    public List<CrawlerTask> searchCrawlerTaskByPermission(SearchCrawlerTaskDTO dto,PageDTO page){
        Example example = new Example(CrawlerTask.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("deleted", 0);
        return taskMapper.selectByExampleAndRowBounds(example,PageUtil.getRowBounds(page));
    }

    @Override
    public Long countCrawlerTaskByPermission(SearchCrawlerTaskDTO dto){
        Example example = new Example(CrawlerTask.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("deleted", 0);
       return Long.valueOf(taskMapper.selectCountByExample(example));
    }
}
